package com.begye.smartloan.apptest.huankuan

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.daohang.DaoHangBottom
import com.begye.smartloan.apptest.databinding.AtHuanKuanBinding
import com.begye.smartloan.apptest.ext.addUnderLine
import com.begye.smartloan.apptest.ext.toThousandUnitFormat
import com.begye.smartloan.apptest.fangkuanzhong.FangKuanZhongActivity
import com.begye.smartloan.apptest.gongong.SysConfigViewModel
import com.begye.smartloan.apptest.gongong.YongHuLiuChengViewModel
import com.begye.smartloan.apptest.homeadsource.HomeAdSourceHuanKuanActivity
import com.begye.smartloan.apptest.jujue.JuJueActivity
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.renzheng.RenZhengActivity
import com.begye.smartloan.apptest.renzheng.bangka.RenZhengBangKaActivity
import com.begye.smartloan.apptest.renzheng.document.RenZhengDocumentActivity
import com.begye.smartloan.apptest.renzheng.lianxiren.RenZhengLianXiRenActivity
import com.begye.smartloan.apptest.renzheng.selfie.RenZhengSelfieActivity
import com.begye.smartloan.apptest.shenhezhong.ShenHeZhongActivity
import com.begye.smartloan.apptest.tool.PageTagTool
import com.begye.smartloan.apptest.tool.SysConfigTool
import com.begye.smartloan.apptest.tool.addTouch
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.openSystemBrowser
import com.util.utillib.extend.toNextActivity


@SuppressLint("CustomWelcomeScreen")
class HuanKuanActivity : AppCompatActivity() {

    private lateinit var _binding: AtHuanKuanBinding

    private val _huanKuanViewModel by lazy {
        ViewModelProvider(this)[HuanKuanViewModel::class.java]
    }

    private val _yongHuLiuChengViewModel by lazy {
        ViewModelProvider(this)[YongHuLiuChengViewModel::class.java]
    }

    private val _sysConfigViewModel by lazy {
        ViewModelProvider(this)[SysConfigViewModel::class.java]
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtHuanKuanBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIEvent()
        configUIData()
    }

    private fun configYongHuLiuChengState() {

        _yongHuLiuChengViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _huanKuanViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuLiuChengViewModel.onHasOrderStatusLiveData.observe(this) {
            it?.let { result ->
                when (result) {
                    PageTagTool.dai_huan_kuan -> {
//                        toNextActivity(
//                            activity = HuanKuanActivity::class.java,
//                            mode = IntentFlagMode.SINGLE_TOP
//                        )
                    }

                    PageTagTool.fang_kuan_zhong -> {
                        toNextActivity(
                            activity = FangKuanZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.shen_he_zhone -> {
                        toNextActivity(
                            activity = ShenHeZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.ju_jue -> {
                        toNextActivity(
                            activity = JuJueActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }
                }
            }
        }

        _yongHuLiuChengViewModel.onDoingRenZhengPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingLianXiRenPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengLianXiRenActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingBangKaFrPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengBangKaActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingDocumentPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengDocumentActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingSelfiePageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengSelfieActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onAllDoneLiveData.observe(this) {
            if (it.isNotBlank()) {
                // to shen qing ye
                toNextActivity(
                    activity = HomeAdSourceHuanKuanActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun configUIState() {

        _huanKuanViewModel.onHuanKuanDetailLiveData.observe(this) {
            it?.let { result ->

                try {
                    _binding.atHuanKuanItemValue1.text =
                        result.optString("repayAmount", "0").toThousandUnitFormat()

                    _binding.atHuanKuanItemValue2.text =
                        result.optString("repayDate", "")

                    _binding.atHuanKuanItemValue3.text =
                        result.optString("amount", "0").toThousandUnitFormat()

                    _binding.atHuanKuanItemValue4.text =
                        result.optString("interest", "0").toThousandUnitFormat()

                    _binding.atHuanKuanItemValue5.text =
                        result.optString("repayProFee", "0").toThousandUnitFormat()

                    _binding.atHuanKuanItemValue7.text =
                        result.optString("overdueAmount", "0").toThousandUnitFormat()

                    var overDay = result.optString("overdueDay", "0")
                    if (overDay.isBlank()) {
                        overDay = "0"
                    }
                    _binding.atHuanKuanItemValue6.text = getString(
                        R.string.huan_kuan_text_34,
                        overDay
                    )

                    if (overDay.toInt() > 0) {
                        _binding.atHuanKuanItemValue6.setTextColor(Color.parseColor("#FF7373"))
                    } else {
                        _binding.atHuanKuanItemValue6.setTextColor(Color.parseColor("#999999"))
                    }

                    //
                    _sysConfigViewModel.requestSystemConfiguration()

                } catch (e: Exception) {
                    //
                }
            }
        }

        _huanKuanViewModel.onRemainDaysLiveData.observe(this) {
            it?.let {
                _binding.atHuanKuanListDaysLeft.text = getString(R.string.huan_kuan_text_1, it)
            }
        }

        _huanKuanViewModel.onHuanKuanUrlLiveData.observe(this) {
            it?.let { result ->
                val isPin = result.optBoolean("isPin", false)
                if (isPin) {
                    val params = HashMap<String, String>().apply {
                        put("orderId", _huanKuanViewModel.pinOrderId)
                        put("thirdOrderId", _huanKuanViewModel.pinThirdOrderId)
                    }
                    toNextActivity(
                        activity = PinHuanKuanProcessingActivity::class.java,
                        mode = IntentFlagMode.SINGLE_TOP,
                        params = params
                    )
                } else {
                    val targetUrl = result.optString("targetUrl", "")
                    if (targetUrl.isNotBlank()) {
                        openSystemBrowser(targetUrl)
                    }
                }
            }
        }

        _sysConfigViewModel.onSysConfigSuccessfulLiveData.observe(this) {
            try {
                val text = SysConfigTool.filterYingHuanKuanImportant()
                if (text.isNotBlank()) {
                    _binding.atHuanKuanTipsText1.text = text
                    _binding.atHuanKuanTipsContent.visibility = View.VISIBLE

                } else {
                    _binding.atHuanKuanTipsContent.visibility = View.GONE
                }
            } catch (e: Exception) {
                //
            }
        }
    }

    private fun configUIEvent() {

        _binding.atHuanKuanNarBar.initConfig(
            leftResId = R.drawable.dres_139,
            rightResId = R.drawable.dres_84
        ) {
            if (0 == it) {
                _binding.atHuanKuanCouTiDrawer.refresh(this@HuanKuanActivity) {
                    if (_binding.atHuanKuanRootLayout.isDrawerOpen(_binding.atHuanKuanCouTiDrawer)) {
                        _binding.atHuanKuanRootLayout.closeDrawer(_binding.atHuanKuanCouTiDrawer)
                    }
                }

                if (_binding.atHuanKuanRootLayout.isDrawerOpen(_binding.atHuanKuanCouTiDrawer)) {
                    _binding.atHuanKuanRootLayout.closeDrawer(_binding.atHuanKuanCouTiDrawer)
                } else {
                    _binding.atHuanKuanRootLayout.openDrawer(_binding.atHuanKuanCouTiDrawer)
                }
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        _binding.atHuanKuanNoneDataToAp.addUnderLine()
        _binding.atHuanKuanNoneDataToAp.setOnClickListener {
            toNextActivity(
                activity = HomeAdSourceHuanKuanActivity::class.java,
                mode = IntentFlagMode.SINGLE_TOP
            )
        }

        _binding.atHuanKuanBottom.initConfig(type = DaoHangBottom.TYPE_HUAN_KUAN) {
            if (it == DaoHangBottom.TYPE_HOME) {
                toNextActivity(
                    activity = HomeAdSourceHuanKuanActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _binding.atHuanKuanListSubmit.setOnClickListener {
            _huanKuanViewModel.requestHuanKuanUrl()

            //
            addTouch(type = "huan_kuan_commit_click")
        }
    }

    private fun configUIData() {
        _huanKuanViewModel.requestHuanKuanList()
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

